package top.wardyan.wtask.jobs;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.jfinal.aop.Aop;
import com.jfinal.kit.Prop;
import com.jfinal.plugin.activerecord.Record;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import top.wardyan.base.common.annotation.Service;
import top.wardyan.base.common.config.ConfigProp;
import top.wardyan.base.common.util.DateUtil;
import top.wardyan.wtask.feishu.api.FeishuMsgBusiness;
import top.wardyan.wtask.service.IProcService;

@Service
public class FeishuCheckJob {

	private static final cn.hutool.log.Log log = cn.hutool.log.Log.get();
	
	// feishu.devmode
	private static final Prop PROP = ConfigProp.load();
	private static final Boolean DEVMODE = PROP.getBoolean("feishu.devmode", false);
	
	/**
	 * 检查最近一个星期以来每天的填报情况
	 * @param min
	 */
	public void deal(Integer min) {
		Timestamp begin = DateUtil.range(17);
		long now = DateUtil.getCurrentTimestamp().getTime();
		long b = begin.getTime();
		long yesterday = DateUtil.range(19).getTime();
		while(b < now) {
			Timestamp date = new Timestamp(b);
			// 如果不是今天昨天,那要求的最少人数就至少是翻倍才提醒
			deal(date,( b < yesterday ? min * 2 : min ) );
			b += 1000 * 60 * 60 * 24;	// 加一天
		}
	}
	
	public void deal(Timestamp today, Integer min) {
		deal(today,min,false);
	}
	public void deal(Timestamp today, Integer min, Boolean force) {
		IProcService procService = Aop.get(IProcService.class);
		FeishuMsgBusiness feishuBusiness = Aop.get(FeishuMsgBusiness.class);		
		if(min == null) {
			min = 1;
		}
		List<Record> empls = procService.yp_listempls(false);
		
		
		List<Record> ts = procService.yp_query_feishu_date("", today);
		Boolean ok = false;
		if(ts != null) {
			List<String> mans = ts.stream().map(new Function<Record,String>(){

				@Override
				public String apply(Record t) {
					return t.getStr("xMan");
				}
				
			}).distinct().collect(Collectors.toList());
			
			if(mans.size() > min) {
				// 至少有3个人就检查
				ok = true;
			}
		}
		if(ok || force) {
			for(Record e : empls) {
				Boolean isNotice = e.getBoolean("xIsNotice");
				if(isNotice) {
				
					String man = e.getStr("xName");
					
					List<Record> tasks = procService.yp_query_feishu_date(man, today);
					
					if(tasks == null || tasks.size() == 0) {
						Console.log("未填飞书任务记录, 日期:", today, man);
						String msg = "未填飞书任务记录, 日期:" + DateUtil.format(today, "yyyy-MM-dd EEEE");
						try {
							if(DEVMODE) {
								Console.log("调试模式消息不需要发出去", man, e.getStr("xCode"), msg);
							}else {
								feishuBusiness.sendToMan(e.getStr("xCode"), msg);
							}
							
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				
				}else {
					Console.log("不需要提醒:", e);
				}
			}
		}
	}

	public void yesterday() {
		// Timestamp today = DateUtil.range(19);
		this.deal(1);
	}
	public void yesterday1() {
		this.yesterday();
	}public void yesterday2() {
		this.yesterday();
	}public void yesterday3() {
		this.yesterday();
	}public void yesterday4() {
		this.yesterday();
	}
	public void today() {
		Timestamp today = DateUtil.range(1);
		this.deal(today, 1);
	}
	
	
	
	
	
	
	
	
	
	

	public void check() {
		Timestamp today = DateUtil.range(19);
		this.deal(today, 1);
	}

	
	public void weekchecking() {
		this.weekcheck(null);
	}
	/**
	 * 每周一早上11:30分把上一周的工时情况发消息给对应人
	 */
	public void weekcheck(String man) {
		if(man == null) {
			man = "";
		}
		String m = man;
		Timestamp date = DateUtil.range(19);
		date = new Timestamp(date.getTime() - 1000 * 60 * 60 * 24 * 2);
		
		IProcService procService = Aop.get(IProcService.class);
		FeishuMsgBusiness feishuMsgBusiness = Aop.get(FeishuMsgBusiness.class);
		
		List<Record> summaries = procService.yp_qry_weeksummary(date);
		if(summaries == null) {
			summaries = new ArrayList<>();
		}
		// 飞书总工时, 预计飞书效率, TAPD总工时, 预计TAPD效率
		List<Record> empls = procService.yp_listempls(false);
		if(empls == null) {
			empls = new ArrayList<>();
		}
		for(Record e : empls) {
			
			String name = e.getStr("xName");
			if(man.equals("") || man.equals(name)) {
				Optional<Record> data = summaries.stream().filter(new Predicate<Record>() {
	
					@Override
					public boolean test(Record t) {
						return  m.equals(t.getStr("xMan"));
					}
					
				}).findFirst();
				
				if(data.isPresent()) {
					Record r = data.get();
					Float feishuhour = r.getFloat("xFeishuTotalHour");
					Float feishuRate = r.getFloat("xFeishuRate");
					Float tapdHour = r.getFloat("xTapdTotalHour");
					Float tapdRate = r.getFloat("xTapdRate");
					Map<String,Float> map = new HashMap<>();
					map.put("feishuhour", feishuhour);
					map.put("feishuRate", feishuRate);
					map.put("tapdHour", tapdHour);
					map.put("tapdRate", tapdRate);
					
					String msg = StrUtil.format("每周一汇总工时提醒:您上个星期飞书工时:{feishuhour}h,估计效率:{feishuRate}%; TAPD工时:{tapdHour}h,估计效率:{tapdRate}%. 如果有未提交任务请尽快提交", map);
					
					try {
						feishuMsgBusiness.sendToMan(e.getStr("xCode"), msg);
					} catch (Exception e1) {
						log.error(e1);
					}
				}

			}
		}
	}
}























